<?php
namespace Album\Model;

use Zend\Db\TableGateway\TableGateway;

class CategoryTable 
{
	protected $tableGateway;
	
	function __construct(TableGateway $tableGateway)
	{
		$this->tableGateway=$tableGateway;
	}
	
	public function fetchAll(){
		$resultSet = $this->tableGateway->select();
		return $resultSet;
	}
	
	public function getAlbum($id)
	{
		$id = (int) $id;
		$rowset = $this->tableGateway->select(array('id'=>$id));
		$row = $rowset->current();
		if (!$row){
			throw new \Exception( "Count not find row $id" );
		}
		return $row;
	}
	
	public function saveAlbum(Album $album){
		$data = array(
			'title'=>$album->title,
			'parent_id' =>$album->parent_id,
		);
		$id = (int) $album->id;
		if ($id==0){
			$this->tableGateway->insert($data);
		}else {
			if ($this->getAlbum($id)){
				$this->tableGateway->update($data,array('id'=>$id));
			}else {
				throw new  \Exception( "Album id does not exist" );
			}
		}
	}
	
	public function deleteAlbum($id)
	{
		$this->tableGateway->delete(array('id'=>$id));
	}
}